import java.io.*;

/**
 * Creates index from a sorted list of "word offset"-entries.
 * 
 * @author Fredrik, Viktor
 */
public class IndexGenerator {
	public static void main(String[] args) {
		if (args.length < 1) {
			printUsage();
		} else {
			makeIndex(args[0]);
		}
	}

	private static void printUsage() {
		System.out.println("Usage: java IndexGenerator filename");
	}

	/**
	 * Generates the files required by IndexSearch.
	 * 
	 * @param inputFile
	 *            A file containing a sorted index off all words in the corpus,
	 *            along with their offsets. ie: serveringen 6029692 serveringen
	 *            9942762 serveringsbord 2405217
	 */
	private static void makeIndex(String inputFile) {
		BufferedReader corpusindex;
		DataOutputStream indexOut, wordOut;
		ObjectOutputStream trieOut;
		FastHashMap trie = new FastHashMap();
		// The "trie" which contains the three first chars and holds pointers to
		// the wordIndex array.
		try {
			// opens the files to be read/written to
			corpusindex = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile), "ISO-8859-1"));
			wordOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("wordfile")));
			indexOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("indexfile")));
			trieOut = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("triefile")));
			indexOut.writeInt(0x1337); // prints a flag at beginning of file
			String lastWord = "", in = "";
			String lastKey = "";
			while ((in = corpusindex.readLine()) != null) {
				String[] data = in.split("\\s");
				if (data[0].length() <= 0)
					continue;
				String key = data[0].substring(0, ((data[0].length() >= 3) ? 3
						: data[0].length())); // get the first 3 chars (if they
												// exist)
				if (lastKey.compareTo(key) != 0) {
					trie.put(key, wordOut.size());
					lastKey = key;
				}
				if (lastWord.compareTo(data[0]) != 0) {
					wordOut.writeUTF(data[0]);
					indexOut.writeInt(-1);
					wordOut.writeInt(indexOut.size());
				}
				indexOut.writeInt(Integer.parseInt(data[1]));
				lastWord = data[0];
			}
			// For the last element.
			indexOut.writeInt(-1);
			trieOut.writeObject(trie);
			trieOut.close();
			wordOut.close();
			indexOut.close();
		} catch (FileNotFoundException e) {
			System.err.println("Input file not found!");
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println("I/O Error!");
			e.printStackTrace();
		}

	}
}
